﻿function BOX_show(e)
{       //显示
        if(document.getElementById(e)==null){return;}
        BOX_layout(e);
        window.onresize = function(){BOX_layout(e);} //改变窗体重新调整位置
        window.onscroll = function(){BOX_layout(e);} //滚动窗体重新调整位置
}

function BOX_remove(e)
{       //移除
        window.onscroll = null;
        window.onresize = null;
        document.getElementById('BOX_overlay').style.display="none";
        document.getElementById(e).style.display="none";
}

function BOX_layout(e)
{       //调整位置
        var a = document.getElementById(e);
        if (document.getElementById('BOX_overlay')==null){ //判断是否新建遮掩层
            var overlay = document.createElement("div");
            overlay.setAttribute('id','BOX_overlay');
            overlay.onclick=function(){BOX_remove(e);};
            a.parentNode.appendChild(overlay);
        }
        //取客户端左上坐标，宽，高
        var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
        var clientWidth;
       // if (window.innerWidth) {
        //    clientWidth = ((Sys.Browser.agent === Sys.Browser.Safari) ? window.innerWidth : Math.min(window.innerWidth, document.documentElement.clientWidth));
       // } else {
            //clientWidth = document.documentElement.clientWidth;
            clientWidth = document.body.clientWidth;
       // }
        var clientHeight;
       // if (window.innerHeight) {
      //      clientHeight = ((Sys.Browser.agent === Sys.Browser.Safari) ? window.innerHeight : Math.min(window.innerHeight, document.documentElement.clientHeight));
       // } else {
            //clientHeight = document.documentElement.clientHeight;
            clientHeight = document.body.clientHeight;
       // }
        var bo = document.getElementById('BOX_overlay');
        bo.style.left = scrollLeft+'px';
        bo.style.top = scrollTop+'px';
        bo.style.width = clientWidth+'px';
        bo.style.height = clientHeight+'px';      
        bo.style.display="";
        //Popup窗口定位
        a.style.position = 'absolute';
        a.style.zIndex=101;
        a.style.display="";
        a.style.left = scrollLeft+((clientWidth-a.offsetWidth)/2)+'px';
        a.style.top = scrollTop+((clientHeight-a.offsetHeight)/2)+'px';
}


function showFriendRequestDialog(userid,username,avatar)
{
     var a = document.createElement("script");
     a.setAttribute('id','AjaxScript');
     a.type = "text/javascript";
     a.src = "/support/Alpha/Js/mootools.js";
     document.getElementsByTagName("BODY")[0].appendChild(a);
     
     var e = document.createElement("div");
     e.setAttribute('id','floatDialog');
//     e.setAttribute('class','friendrequestbox'):
     e.innerHTML = "<div class=\"friendrequestbox\"><h2><span>添加好友</span></h2><div class=\"dialog_body\"><div class=\"friend-request\"><div class=\"avatar\"><img src=\""+avatar+"\"><p style=\"text-align:center;\">"+username+"</p></div>"+"<div class=\"msg\" id=\"addFriendAlert\">发送好友申请后，对方会收到提示，经对方确认后，你们即可成为好友。<br><br><br><div style=\"display: block;\" id=\"addMsgBox\"><p>添加一条个人信息<font color=\"gray\">（选填）</font>：</p><textarea id=\"addFriendMessage\" name=\"message\" class=\"textarea\" cols=\"30\" rows=\"3\" style=\"width: 270px;\"></textarea></div></div></div></div><div class=\"dialog_buttons\">"
     +"<input id=\"submitbutton\" value=\"确定\" class=\"input-submit\" type=\"button\" onclick=\"AddFriend("+userid+");\"><input value=\"取消\" class=\"input-submit gray\" type=\"button\" onclick=\"BOX_remove(\'floatDialog\');removeElement();\"></div></div>";
     document.getElementsByTagName("BODY")[0].appendChild(e);
     BOX_show('floatDialog');
 }
 
function removeElement()
{

     var e = document.getElementById('floatDialog');
     var a = document.getElementById('AjaxScript');
     document.getElementsByTagName("BODY")[0].removeChild(e);
     document.getElementsByTagName("BODY")[0].removeChild(a);
}

function AddFriend(userid)
{
    
    var msg = $("addFriendMessage").value;
  
    if(msg.length > 300 ) 
    {
        alert("评论内容不能超过100个字！");
        return;
    }
  
  $("submitbutton").value = "正在处理...";
  var aurl = "/Pages/alpha/User/FriendAdd.aspx"+"?uid="+userid+"&msg="+msg.escUrl();
  var myAjax=new Ajax(aurl,
  {
     method:'post',
     onComplete:function()
     {
              alert(this.response.text);
              BOX_remove('floatDialog');
              removeElement();
      }
   }
   );
   myAjax.request();
  
  
}